# -*- coding:utf-8 -*-

# 参考python官网文档

# timedelta对象的用法
"""
from datetime import timedelta

year = timedelta(days=365)
print(year)
print(year.total_seconds())
print('=='*10)

another_year = timedelta(weeks=40,days=84,hours=23,minutes=50,seconds=600)
print(another_year)

ten_years = 10 * year
print(ten_years,ten_years.days)

print('-'*20)
nine_years = ten_years - year
print(nine_years,nine_years.days)

print('===='*20)
three_years = nine_years 
print(three_years,three_years.days)
print(abs(three_years - ten_years) == 2 * three_years + year)
'''
输出结果为:
365 days, 0:00:00
31536000.0
====================
365 days, 0:00:00
3650 days, 0:00:00 3650
--------------------
3285 days, 0:00:00 3285
================================================================================
3285 days, 0:00:00 3285
False
'''
"""

# date对象, 例如:计算事件天数
'''
import time
from datetime import date
today = date.today()
print(today)
print('==' * 10)
today = date.fromtimestamp(time.time())
print(today)
print('--' * 10)

birthday = date(today.year,5,20)
print(birthday)
print('++' * 10)
if birthday < today:
    birthday = birthday.replace(year=today.year + 1)
    print(birthday)

time_to_birthday = abs(birthday - today)
print(time_to_birthday.days)
'''
"""
from datetime import date

d = date.fromordinal(730920)
print(d)
t = d.timetuple()
for i in t:
    print(i)

ic = d.isocalendar()
for i in ic:
    print(i)

print(d.isoformat())
print(d.strftime("%d/%m/%y"))
print(d.strftime("%A %d. %B %Y"))
print('The {1} is {0:%d},the {2} is {0:%B}.'.format(d,"day","month"))

'''
输出结果为:
2002-03-11
2002
3
11
0
0
0
0
70
-1
2002
11
1
2002-03-11
11/03/02
Monday 11. March 2002
The day is 11,the month is March.
'''
"""

# datetime.isoformat（sep ='T'，timespec ='auto')用法:
from datetime import tzinfo,timedelta,datetime
class TZ(tzinfo):
    def utcoffset(self,dt): return timedelta(minutes=-30)

print(datetime(2020,1,8,tzinfo=TZ()).isoformat(' '))
# 输出结果为:2020-01-08 00:00:00-00:30

'''
可选参数timespec指定要包括的时间的其他组件的数量（默认值为'auto'）。它可以是以下之一：

'auto'：'seconds'如果microsecond是0，则相同'microseconds'。
'hours'：包括hour两位数的HH格式。
'minutes'：包括hour和minute以HH：MM格式。
'seconds'：包含hour，minute和second 以HH：MM：SS格式。
'milliseconds'：包括全部时间，但将小数部分截断为毫秒。HH：MM：SS.sss格式。
'microseconds'：包括HH：MM：SS.mmmmmm格式的全职
'''

from datetime import datetime
print(datetime.now().isoformat(timespec='minutes'))

dt = datetime(2020,1,8,12,31,59,0)
print(dt.isoformat(timespec='microseconds'))
'''
输出结果为:
2019-01-08T18:13
2020-01-08T12:31:59.000000
'''


# 使用datetime对象的示例:
from datetime import datetime,date,time

d = date(2019,1,8)
t = time(12,31)
# Using datetime.combine()
print(datetime.combine(d,t))

# Using datetime.now() or datetime.utcnow()
print(datetime.now())
print(datetime.utcnow())

 # Using datetime.strptime()
dt = datetime.strptime("19/1/08 18:18","%d/%m/%y %H:%M")
print(dt)

#  Using datetime.timetuple() to get tuple of all attributes
tt = dt.timetuple()
for it in tt:
    print(it)

# Date in ISO format
ic = dt.isocalendar()
for ica in ic:
    print(ica)

# formatting datetime
print(dt.strftime("%A,%d. %B %Y %I:%M%p"))

print("THe {1} is {0:%d},the {2} is {0:%B},the {3} is {0:%I:%M%p}".format(dt,"day","month","time"))
'''
输出结果如下:
2019-01-08 12:31:00
2019-01-08 18:22:43.617593
2019-01-08 10:22:43.617600
2008-01-19 18:18:00
2008
1
19
18
18
0
5
19
-1
2008
3
6
Saturday,19. January 2008 06:18PM
THe day is 19,the month is January,the time is 06:18PM
'''

from datetime import time,tzinfo,timedelta

class GMT1(tzinfo):
    def utcoffset(self,dt):
        return timedelta(hours=1)
    def dst(self,dt):
        return timedelta(0)
    def tzname(self,dt):
        return "Europe/Prague"

t = time(12,10,30,tzinfo=GMT1())
print(t)
gmt = GMT1()
print(t.isoformat())
print(t.dst())
print(t.tzname())
print(t.strftime("%H:%M:%S %Z"))
print('The {} is {:%H:%M}.'.format("time",t))











